// 这里不需要引入jquery ，可以避免版本冲突，但在引入这个js时，请确保已经引入jquery
export function POST(url,data,header){
    // 监听 Form 表单提交事件
    return new Promise((resolve,reject)=> {
        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(data),
            async: false,
            headers: {"Content-Type": "application/json;charset=UTF-8", ...header},
            dataType: 'json',//服务器返回json格式数据
            success: function (data) {
                resolve(data)
            }, error: function (e) {
                reject(e)
            }
        });
    })
}
export function POST_FORM(url,data={},header){
    // 监听 Form 表单提交事件
    return new Promise((resolve,reject)=> {
        let formData = new FormData();
        for (let key in data) {
            formData.append(key, data[key]);
        }
        $.ajax({
            url: url,
            type: 'POST',
            async: false,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            header: {...header},
            success: function (data) {
                resolve(data)
            }, error: function (e) {
                reject(e)
            }
        });
    })
}
export function GET(url,query,header={}){
    function queryToUrl(url,query) {
        if (typeof query != 'object') return url;
        let keys = Object.keys(query);
        if (keys.length == 0 ) return url;
        let isHead = true;
        keys.forEach(key=>{
            if (isHead) {
                url += `?${key}=${query[key]}`
                isHead = false
            }else {
                url += `&${key}=${query[key]}`
            }
        })
        return url;
    }
    // 监听 Form 表单提交事件
    return new Promise((resolve,reject)=> {
        $.ajax({
            type: "get",
            url: queryToUrl(url,query),
            async: false,
            header: header,
            dataType: 'json',//服务器返回json格式数据
            success: function (data) {
                resolve(data)
            }, error: function (e) {
                reject(e)
            }
        });
    })
}